<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link rel="stylesheet" type="text/css" href="style.css" /><script type="text/javascript" src="highlight.js"></script></head><body><pre><span class="hs-pragma">{-# LANGUAGE Trustworthy #-}</span><span>
</span><span id="line-2"></span><span class="hs-pragma">{-# LANGUAGE NoImplicitPrelude #-}</span><span>
</span><span id="line-3"></span><span>
</span><span id="line-4"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-5"></span><span class="hs-comment">-- |</span><span>
</span><span id="line-6"></span><span class="hs-comment">-- Module      :  Control.Monad</span><span>
</span><span id="line-7"></span><span class="hs-comment">-- Copyright   :  (c) The University of Glasgow 2001</span><span>
</span><span id="line-8"></span><span class="hs-comment">-- License     :  BSD-style (see the file libraries/base/LICENSE)</span><span>
</span><span id="line-9"></span><span class="hs-comment">--</span><span>
</span><span id="line-10"></span><span class="hs-comment">-- Maintainer  :  libraries@haskell.org</span><span>
</span><span id="line-11"></span><span class="hs-comment">-- Stability   :  provisional</span><span>
</span><span id="line-12"></span><span class="hs-comment">-- Portability :  portable</span><span>
</span><span id="line-13"></span><span class="hs-comment">--</span><span>
</span><span id="line-14"></span><span class="hs-comment">-- The 'Functor', 'Monad' and 'MonadPlus' classes,</span><span>
</span><span id="line-15"></span><span class="hs-comment">-- with some useful operations on monads.</span><span>
</span><span id="line-16"></span><span>
</span><span id="line-17"></span><span class="hs-keyword">module</span><span> </span><span class="hs-identifier">Control.Monad</span><span>
</span><span id="line-18"></span><span>    </span><span class="hs-special">(</span><span>
</span><span id="line-19"></span><span>    </span><span class="annot"><span class="hs-comment">-- * Functor and monad classes</span></span><span>
</span><span id="line-20"></span><span>
</span><span id="line-21"></span><span>      </span><span class="annot"><a href="GHC.Base.html#Functor"><span class="hs-identifier">Functor</span></a></span><span class="hs-special">(</span><span class="hs-glyph">..</span><span class="hs-special">)</span><span>
</span><span id="line-22"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.Base.html#Monad"><span class="hs-identifier">Monad</span></a></span><span class="hs-special">(</span><span class="annot"><a href="GHC.Base.html#%3E%3E%3D"><span class="hs-operator">(&gt;&gt;=)</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.Base.html#%3E%3E"><span class="hs-operator">(&gt;&gt;)</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.Base.html#return"><span class="hs-identifier">return</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-23"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Control.Monad.Fail.html#MonadFail"><span class="hs-identifier">MonadFail</span></a></span><span class="hs-special">(</span><span class="annot"><a href="Control.Monad.Fail.html#fail"><span class="hs-identifier">fail</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-24"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.Base.html#MonadPlus"><span class="hs-identifier">MonadPlus</span></a></span><span class="hs-special">(</span><span class="annot"><a href="GHC.Base.html#mzero"><span class="hs-identifier">mzero</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.Base.html#mplus"><span class="hs-identifier">mplus</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-25"></span><span>    </span><span class="annot"><span class="hs-comment">-- * Functions</span></span><span>
</span><span id="line-26"></span><span>
</span><span id="line-27"></span><span>    </span><span class="annot"><span class="hs-comment">-- ** Naming conventions</span></span><span>
</span><span id="line-28"></span><span>    </span><span class="annot"><span class="hs-comment">-- $naming</span></span><span>
</span><span id="line-29"></span><span>
</span><span id="line-30"></span><span>    </span><span class="annot"><span class="hs-comment">-- ** Basic @Monad@ functions</span></span><span>
</span><span id="line-31"></span><span>
</span><span id="line-32"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Traversable.html#mapM"><span class="hs-identifier">mapM</span></a></span><span>
</span><span id="line-33"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Foldable.html#mapM_"><span class="hs-identifier">mapM_</span></a></span><span>
</span><span id="line-34"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Traversable.html#forM"><span class="hs-identifier">forM</span></a></span><span>
</span><span id="line-35"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Foldable.html#forM_"><span class="hs-identifier">forM_</span></a></span><span>
</span><span id="line-36"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Traversable.html#sequence"><span class="hs-identifier">sequence</span></a></span><span>
</span><span id="line-37"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Foldable.html#sequence_"><span class="hs-identifier">sequence_</span></a></span><span>
</span><span id="line-38"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.Base.html#%3D%3C%3C"><span class="hs-operator">(=&lt;&lt;)</span></a></span><span>
</span><span id="line-39"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Control.Monad.html#%3E%3D%3E"><span class="hs-operator">(&gt;=&gt;)</span></a></span><span>
</span><span id="line-40"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Control.Monad.html#%3C%3D%3C"><span class="hs-operator">(&lt;=&lt;)</span></a></span><span>
</span><span id="line-41"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Control.Monad.html#forever"><span class="hs-identifier">forever</span></a></span><span>
</span><span id="line-42"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Functor.html#void"><span class="hs-identifier">void</span></a></span><span>
</span><span id="line-43"></span><span>
</span><span id="line-44"></span><span>    </span><span class="annot"><span class="hs-comment">-- ** Generalisations of list functions</span></span><span>
</span><span id="line-45"></span><span>
</span><span id="line-46"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.Base.html#join"><span class="hs-identifier">join</span></a></span><span>
</span><span id="line-47"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Foldable.html#msum"><span class="hs-identifier">msum</span></a></span><span>
</span><span id="line-48"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Control.Monad.html#mfilter"><span class="hs-identifier">mfilter</span></a></span><span>
</span><span id="line-49"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Control.Monad.html#filterM"><span class="hs-identifier">filterM</span></a></span><span>
</span><span id="line-50"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Control.Monad.html#mapAndUnzipM"><span class="hs-identifier">mapAndUnzipM</span></a></span><span>
</span><span id="line-51"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Control.Monad.html#zipWithM"><span class="hs-identifier">zipWithM</span></a></span><span>
</span><span id="line-52"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Control.Monad.html#zipWithM_"><span class="hs-identifier">zipWithM_</span></a></span><span>
</span><span id="line-53"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Control.Monad.html#foldM"><span class="hs-identifier">foldM</span></a></span><span>
</span><span id="line-54"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Control.Monad.html#foldM_"><span class="hs-identifier">foldM_</span></a></span><span>
</span><span id="line-55"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Control.Monad.html#replicateM"><span class="hs-identifier">replicateM</span></a></span><span>
</span><span id="line-56"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Control.Monad.html#replicateM_"><span class="hs-identifier">replicateM_</span></a></span><span>
</span><span id="line-57"></span><span>
</span><span id="line-58"></span><span>    </span><span class="annot"><span class="hs-comment">-- ** Conditional execution of monadic expressions</span></span><span>
</span><span id="line-59"></span><span>
</span><span id="line-60"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Control.Monad.html#guard"><span class="hs-identifier">guard</span></a></span><span>
</span><span id="line-61"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.Base.html#when"><span class="hs-identifier">when</span></a></span><span>
</span><span id="line-62"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Control.Monad.html#unless"><span class="hs-identifier">unless</span></a></span><span>
</span><span id="line-63"></span><span>
</span><span id="line-64"></span><span>    </span><span class="annot"><span class="hs-comment">-- ** Monadic lifting operators</span></span><span>
</span><span id="line-65"></span><span>
</span><span id="line-66"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.Base.html#liftM"><span class="hs-identifier">liftM</span></a></span><span>
</span><span id="line-67"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.Base.html#liftM2"><span class="hs-identifier">liftM2</span></a></span><span>
</span><span id="line-68"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.Base.html#liftM3"><span class="hs-identifier">liftM3</span></a></span><span>
</span><span id="line-69"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.Base.html#liftM4"><span class="hs-identifier">liftM4</span></a></span><span>
</span><span id="line-70"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.Base.html#liftM5"><span class="hs-identifier">liftM5</span></a></span><span>
</span><span id="line-71"></span><span>
</span><span id="line-72"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.Base.html#ap"><span class="hs-identifier">ap</span></a></span><span>
</span><span id="line-73"></span><span>
</span><span id="line-74"></span><span>    </span><span class="annot"><span class="hs-comment">-- ** Strict monadic functions</span></span><span>
</span><span id="line-75"></span><span>
</span><span id="line-76"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Control.Monad.html#%3C%24%21%3E"><span class="hs-operator">(&lt;$!&gt;)</span></a></span><span>
</span><span id="line-77"></span><span>    </span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-78"></span><span>
</span><span id="line-79"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Control.Monad.Fail.html"><span class="hs-identifier">Control.Monad.Fail</span></a></span><span> </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="Control.Monad.Fail.html#MonadFail"><span class="hs-identifier">MonadFail</span></a></span><span class="hs-special">(</span><span class="annot"><a href="Control.Monad.Fail.html#fail"><span class="hs-identifier">fail</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-80"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Data.Foldable.html"><span class="hs-identifier">Data.Foldable</span></a></span><span> </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="Data.Foldable.html#Foldable"><span class="hs-identifier">Foldable</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Foldable.html#sequence_"><span class="hs-identifier">sequence_</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Foldable.html#sequenceA_"><span class="hs-identifier">sequenceA_</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Foldable.html#msum"><span class="hs-identifier">msum</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Foldable.html#mapM_"><span class="hs-identifier">mapM_</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Foldable.html#foldlM"><span class="hs-identifier">foldlM</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Foldable.html#forM_"><span class="hs-identifier">forM_</span></a></span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-81"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Data.Functor.html"><span class="hs-identifier">Data.Functor</span></a></span><span> </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="Data.Functor.html#void"><span class="hs-identifier">void</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Functor.html#%3C%24%3E"><span class="hs-operator">(&lt;$&gt;)</span></a></span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-82"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Data.Traversable.html"><span class="hs-identifier">Data.Traversable</span></a></span><span> </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="Data.Traversable.html#forM"><span class="hs-identifier">forM</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Traversable.html#mapM"><span class="hs-identifier">mapM</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Traversable.html#traverse"><span class="hs-identifier">traverse</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Traversable.html#sequence"><span class="hs-identifier">sequence</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Traversable.html#sequenceA"><span class="hs-identifier">sequenceA</span></a></span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-83"></span><span>
</span><span id="line-84"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Base.html"><span class="hs-identifier">GHC.Base</span></a></span><span> </span><span class="hs-keyword">hiding</span><span> </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="GHC.Base.html#mapM"><span class="hs-identifier">mapM</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.Base.html#sequence"><span class="hs-identifier">sequence</span></a></span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-85"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.List.html"><span class="hs-identifier">GHC.List</span></a></span><span> </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="GHC.List.html#zipWith"><span class="hs-identifier">zipWith</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.List.html#unzip"><span class="hs-identifier">unzip</span></a></span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-86"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Num.html"><span class="hs-identifier">GHC.Num</span></a></span><span>  </span><span class="hs-special">(</span><span> </span><span class="hs-special">(</span><span class="hs-glyph">-</span><span class="hs-special">)</span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-87"></span><span>
</span><span id="line-88"></span><span class="hs-comment">-- -----------------------------------------------------------------------------</span><span>
</span><span id="line-89"></span><span class="hs-comment">-- Functions mandated by the Prelude</span><span>
</span><span id="line-90"></span><span>
</span><span id="line-91"></span><span class="hs-comment">-- | Conditional failure of 'Alternative' computations. Defined by</span><span>
</span><span id="line-92"></span><span class="hs-comment">--</span><span>
</span><span id="line-93"></span><span class="hs-comment">-- @</span><span>
</span><span id="line-94"></span><span class="hs-comment">-- guard True  = 'pure' ()</span><span>
</span><span id="line-95"></span><span class="hs-comment">-- guard False = 'empty'</span><span>
</span><span id="line-96"></span><span class="hs-comment">-- @</span><span>
</span><span id="line-97"></span><span class="hs-comment">--</span><span>
</span><span id="line-98"></span><span class="hs-comment">-- ==== __Examples__</span><span>
</span><span id="line-99"></span><span class="hs-comment">--</span><span>
</span><span id="line-100"></span><span class="hs-comment">-- Common uses of 'guard' include conditionally signaling an error in</span><span>
</span><span id="line-101"></span><span class="hs-comment">-- an error monad and conditionally rejecting the current choice in an</span><span>
</span><span id="line-102"></span><span class="hs-comment">-- 'Alternative'-based parser.</span><span>
</span><span id="line-103"></span><span class="hs-comment">--</span><span>
</span><span id="line-104"></span><span class="hs-comment">-- As an example of signaling an error in the error monad 'Maybe',</span><span>
</span><span id="line-105"></span><span class="hs-comment">-- consider a safe division function @safeDiv x y@ that returns</span><span>
</span><span id="line-106"></span><span class="hs-comment">-- 'Nothing' when the denominator @y@ is zero and @'Just' (x \`div\`</span><span>
</span><span id="line-107"></span><span class="hs-comment">-- y)@ otherwise. For example:</span><span>
</span><span id="line-108"></span><span class="hs-comment">--</span><span>
</span><span id="line-109"></span><span class="hs-comment">-- @</span><span>
</span><span id="line-110"></span><span class="hs-comment">-- &gt;&gt;&gt; safeDiv 4 0</span><span>
</span><span id="line-111"></span><span class="hs-comment">-- Nothing</span><span>
</span><span id="line-112"></span><span class="hs-comment">-- &gt;&gt;&gt; safeDiv 4 2</span><span>
</span><span id="line-113"></span><span class="hs-comment">-- Just 2</span><span>
</span><span id="line-114"></span><span class="hs-comment">-- @</span><span>
</span><span id="line-115"></span><span class="hs-comment">--</span><span>
</span><span id="line-116"></span><span class="hs-comment">-- A definition of @safeDiv@ using guards, but not 'guard':</span><span>
</span><span id="line-117"></span><span class="hs-comment">--</span><span>
</span><span id="line-118"></span><span class="hs-comment">-- @</span><span>
</span><span id="line-119"></span><span class="hs-comment">-- safeDiv :: Int -&gt; Int -&gt; Maybe Int</span><span>
</span><span id="line-120"></span><span class="hs-comment">-- safeDiv x y | y /= 0    = Just (x \`div\` y)</span><span>
</span><span id="line-121"></span><span class="hs-comment">--             | otherwise = Nothing</span><span>
</span><span id="line-122"></span><span class="hs-comment">-- @</span><span>
</span><span id="line-123"></span><span class="hs-comment">--</span><span>
</span><span id="line-124"></span><span class="hs-comment">-- A definition of @safeDiv@ using 'guard' and 'Monad' @do@-notation:</span><span>
</span><span id="line-125"></span><span class="hs-comment">--</span><span>
</span><span id="line-126"></span><span class="hs-comment">-- @</span><span>
</span><span id="line-127"></span><span class="hs-comment">-- safeDiv :: Int -&gt; Int -&gt; Maybe Int</span><span>
</span><span id="line-128"></span><span class="hs-comment">-- safeDiv x y = do</span><span>
</span><span id="line-129"></span><span class="hs-comment">--   guard (y /= 0)</span><span>
</span><span id="line-130"></span><span class="hs-comment">--   return (x \`div\` y)</span><span>
</span><span id="line-131"></span><span class="hs-comment">-- @</span><span>
</span><span id="line-132"></span><span id="local-6989586621679553552"><span class="annot"><a href="Control.Monad.html#guard"><span class="hs-identifier hs-type">guard</span></a></span><span>           </span><span class="hs-glyph">::</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Base.html#Alternative"><span class="hs-identifier hs-type">Alternative</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553552"><span class="hs-identifier hs-type">f</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Bool"><span class="hs-identifier hs-type">Bool</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553552"><span class="hs-identifier hs-type">f</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span></span><span>
</span><span id="line-133"></span><span id="guard"><span class="annot"><span class="annottext">guard :: forall (f :: * -&gt; *). Alternative f =&gt; Bool -&gt; f ()
</span><a href="Control.Monad.html#guard"><span class="hs-identifier hs-var hs-var">guard</span></a></span></span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="../../ghc-prim/src/GHC.Types.html#True"><span class="hs-identifier hs-var">True</span></a></span><span>      </span><span class="hs-glyph">=</span><span>  </span><span class="annot"><span class="annottext">() -&gt; f ()
forall (f :: * -&gt; *) a. Applicative f =&gt; a -&gt; f a
</span><a href="GHC.Base.html#pure"><span class="hs-identifier hs-var">pure</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-134"></span><span class="annot"><a href="Control.Monad.html#guard"><span class="hs-identifier hs-var">guard</span></a></span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="../../ghc-prim/src/GHC.Types.html#False"><span class="hs-identifier hs-var">False</span></a></span><span>     </span><span class="hs-glyph">=</span><span>  </span><span class="annot"><span class="annottext">f ()
forall (f :: * -&gt; *) a. Alternative f =&gt; f a
</span><a href="GHC.Base.html#empty"><span class="hs-identifier hs-var">empty</span></a></span><span>
</span><span id="line-135"></span><span>
</span><span id="line-136"></span><span class="hs-comment">-- | This generalizes the list-based 'Data.List.filter' function.</span><span>
</span><span id="line-137"></span><span>
</span><span id="line-138"></span><span class="hs-pragma">{-# INLINE</span><span> </span><span class="annot"><a href="Control.Monad.html#filterM"><span class="hs-pragma hs-type">filterM</span></a></span><span> </span><span class="hs-pragma">#-}</span><span>
</span><span id="line-139"></span><span id="local-6989586621679553544"><span id="local-6989586621679553545"><span class="annot"><a href="Control.Monad.html#filterM"><span class="hs-identifier hs-type">filterM</span></a></span><span>          </span><span class="hs-glyph">::</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Base.html#Applicative"><span class="hs-identifier hs-type">Applicative</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553545"><span class="hs-identifier hs-type">m</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679553544"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553545"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Bool"><span class="hs-identifier hs-type">Bool</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="#local-6989586621679553544"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">]</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553545"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="hs-special">[</span><span class="annot"><a href="#local-6989586621679553544"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">]</span></span></span><span>
</span><span id="line-140"></span><span id="filterM"><span class="annot"><span class="annottext">filterM :: forall (m :: * -&gt; *) a.
Applicative m =&gt;
(a -&gt; m Bool) -&gt; [a] -&gt; m [a]
</span><a href="Control.Monad.html#filterM"><span class="hs-identifier hs-var hs-var">filterM</span></a></span></span><span> </span><span id="local-6989586621679553360"><span class="annot"><span class="annottext">a -&gt; m Bool
</span><a href="#local-6989586621679553360"><span class="hs-identifier hs-var">p</span></a></span></span><span>        </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(a -&gt; m [a] -&gt; m [a]) -&gt; m [a] -&gt; [a] -&gt; m [a]
forall a b. (a -&gt; b -&gt; b) -&gt; b -&gt; [a] -&gt; b
</span><a href="GHC.Base.html#foldr"><span class="hs-identifier hs-var">foldr</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span> </span><span id="local-6989586621679553359"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679553359"><span class="hs-identifier hs-var">x</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">(Bool -&gt; [a] -&gt; [a]) -&gt; m Bool -&gt; m [a] -&gt; m [a]
forall (f :: * -&gt; *) a b c.
Applicative f =&gt;
(a -&gt; b -&gt; c) -&gt; f a -&gt; f b -&gt; f c
</span><a href="GHC.Base.html#liftA2"><span class="hs-identifier hs-var">liftA2</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span> </span><span id="local-6989586621679553357"><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679553357"><span class="hs-identifier hs-var">flg</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">if</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679553357"><span class="hs-identifier hs-var">flg</span></a></span><span> </span><span class="hs-keyword">then</span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679553359"><span class="hs-identifier hs-var">x</span></a></span><span class="annot"><span class="annottext">a -&gt; [a] -&gt; [a]
forall a. a -&gt; [a] -&gt; [a]
</span><a href="../../ghc-prim/src/GHC.Types.html#%3A"><span class="hs-glyph hs-var">:</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-keyword">else</span><span> </span><span class="annot"><span class="annottext">[a] -&gt; [a]
forall a. a -&gt; a
</span><a href="GHC.Base.html#id"><span class="hs-identifier hs-var">id</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">a -&gt; m Bool
</span><a href="#local-6989586621679553360"><span class="hs-identifier hs-var">p</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679553359"><span class="hs-identifier hs-var">x</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">[a] -&gt; m [a]
forall (f :: * -&gt; *) a. Applicative f =&gt; a -&gt; f a
</span><a href="GHC.Base.html#pure"><span class="hs-identifier hs-var">pure</span></a></span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span class="hs-special">)</span><span>
</span><span id="line-141"></span><span>
</span><span id="line-142"></span><span class="hs-keyword">infixr</span><span> </span><span class="hs-number">1</span><span> </span><span class="annot"><a href="Control.Monad.html#%3C%3D%3C"><span class="hs-operator hs-type">&lt;=&lt;</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Control.Monad.html#%3E%3D%3E"><span class="hs-operator hs-type">&gt;=&gt;</span></a></span><span>
</span><span id="line-143"></span><span>
</span><span id="line-144"></span><span class="hs-comment">-- | Left-to-right composition of Kleisli arrows.</span><span>
</span><span id="line-145"></span><span class="hs-comment">--</span><span>
</span><span id="line-146"></span><span class="hs-comment">-- \'@(bs '&gt;=&gt;' cs) a@\' can be understood as the @do@ expression</span><span>
</span><span id="line-147"></span><span class="hs-comment">--</span><span>
</span><span id="line-148"></span><span class="hs-comment">-- @</span><span>
</span><span id="line-149"></span><span class="hs-comment">-- do b &lt;- bs a</span><span>
</span><span id="line-150"></span><span class="hs-comment">--    cs b</span><span>
</span><span id="line-151"></span><span class="hs-comment">-- @</span><span>
</span><span id="line-152"></span><span id="local-6989586621679553531"><span id="local-6989586621679553532"><span id="local-6989586621679553533"><span id="local-6989586621679553534"><span class="annot"><a href="Control.Monad.html#%3E%3D%3E"><span class="hs-operator hs-type">(&gt;=&gt;)</span></a></span><span>       </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.Base.html#Monad"><span class="hs-identifier hs-type">Monad</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553534"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679553533"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553534"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553532"><span class="hs-identifier hs-type">b</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679553532"><span class="hs-identifier hs-type">b</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553534"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553531"><span class="hs-identifier hs-type">c</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679553533"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553534"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553531"><span class="hs-identifier hs-type">c</span></a></span><span class="hs-special">)</span></span></span></span></span><span>
</span><span id="line-153"></span><span id="local-6989586621679553353"><span class="annot"><span class="annottext">a -&gt; m b
</span><a href="#local-6989586621679553353"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span id="%3E%3D%3E"><span class="annot"><span class="annottext">&gt;=&gt; :: forall (m :: * -&gt; *) a b c.
Monad m =&gt;
(a -&gt; m b) -&gt; (b -&gt; m c) -&gt; a -&gt; m c
</span><a href="Control.Monad.html#%3E%3D%3E"><span class="hs-operator hs-var hs-var">&gt;=&gt;</span></a></span></span><span> </span><span id="local-6989586621679553352"><span class="annot"><span class="annottext">b -&gt; m c
</span><a href="#local-6989586621679553352"><span class="hs-identifier hs-var">g</span></a></span></span><span>     </span><span class="hs-glyph">=</span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679553351"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679553351"><span class="hs-identifier hs-var">x</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">a -&gt; m b
</span><a href="#local-6989586621679553353"><span class="hs-identifier hs-var">f</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679553351"><span class="hs-identifier hs-var">x</span></a></span><span> </span><span class="annot"><span class="annottext">m b -&gt; (b -&gt; m c) -&gt; m c
forall (m :: * -&gt; *) a b. Monad m =&gt; m a -&gt; (a -&gt; m b) -&gt; m b
</span><a href="GHC.Base.html#%3E%3E%3D"><span class="hs-operator hs-var">&gt;&gt;=</span></a></span><span> </span><span class="annot"><span class="annottext">b -&gt; m c
</span><a href="#local-6989586621679553352"><span class="hs-identifier hs-var">g</span></a></span><span>
</span><span id="line-154"></span><span>
</span><span id="line-155"></span><span class="hs-comment">-- | Right-to-left composition of Kleisli arrows. @('&gt;=&gt;')@, with the arguments</span><span>
</span><span id="line-156"></span><span class="hs-comment">-- flipped.</span><span>
</span><span id="line-157"></span><span class="hs-comment">--</span><span>
</span><span id="line-158"></span><span class="hs-comment">-- Note how this operator resembles function composition @('.')@:</span><span>
</span><span id="line-159"></span><span class="hs-comment">--</span><span>
</span><span id="line-160"></span><span class="hs-comment">-- &gt; (.)   ::            (b -&gt;   c) -&gt; (a -&gt;   b) -&gt; a -&gt;   c</span><span>
</span><span id="line-161"></span><span class="hs-comment">-- &gt; (&lt;=&lt;) :: Monad m =&gt; (b -&gt; m c) -&gt; (a -&gt; m b) -&gt; a -&gt; m c</span><span>
</span><span id="line-162"></span><span id="local-6989586621679553520"><span id="local-6989586621679553521"><span id="local-6989586621679553522"><span id="local-6989586621679553523"><span class="annot"><a href="Control.Monad.html#%3C%3D%3C"><span class="hs-operator hs-type">(&lt;=&lt;)</span></a></span><span>       </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.Base.html#Monad"><span class="hs-identifier hs-type">Monad</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553523"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679553522"><span class="hs-identifier hs-type">b</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553523"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553521"><span class="hs-identifier hs-type">c</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679553520"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553523"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553522"><span class="hs-identifier hs-type">b</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679553520"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553523"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553521"><span class="hs-identifier hs-type">c</span></a></span><span class="hs-special">)</span></span></span></span></span><span>
</span><span id="line-163"></span><span id="%3C%3D%3C"><span class="annot"><span class="annottext">&lt;=&lt; :: forall (m :: * -&gt; *) b c a.
Monad m =&gt;
(b -&gt; m c) -&gt; (a -&gt; m b) -&gt; a -&gt; m c
</span><a href="Control.Monad.html#%3C%3D%3C"><span class="hs-operator hs-var hs-var">(&lt;=&lt;)</span></a></span></span><span>       </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">((a -&gt; m b) -&gt; (b -&gt; m c) -&gt; a -&gt; m c)
-&gt; (b -&gt; m c) -&gt; (a -&gt; m b) -&gt; a -&gt; m c
forall a b c. (a -&gt; b -&gt; c) -&gt; b -&gt; a -&gt; c
</span><a href="GHC.Base.html#flip"><span class="hs-identifier hs-var">flip</span></a></span><span> </span><span class="annot"><span class="annottext">(a -&gt; m b) -&gt; (b -&gt; m c) -&gt; a -&gt; m c
forall (m :: * -&gt; *) a b c.
Monad m =&gt;
(a -&gt; m b) -&gt; (b -&gt; m c) -&gt; a -&gt; m c
</span><a href="Control.Monad.html#%3E%3D%3E"><span class="hs-operator hs-var">(&gt;=&gt;)</span></a></span><span>
</span><span id="line-164"></span><span>
</span><span id="line-165"></span><span class="hs-comment">-- | Repeat an action indefinitely.</span><span>
</span><span id="line-166"></span><span class="hs-comment">--</span><span>
</span><span id="line-167"></span><span class="hs-comment">-- ==== __Examples__</span><span>
</span><span id="line-168"></span><span class="hs-comment">--</span><span>
</span><span id="line-169"></span><span class="hs-comment">-- A common use of 'forever' is to process input from network sockets,</span><span>
</span><span id="line-170"></span><span class="hs-comment">-- 'System.IO.Handle's, and channels</span><span>
</span><span id="line-171"></span><span class="hs-comment">-- (e.g. 'Control.Concurrent.MVar.MVar' and</span><span>
</span><span id="line-172"></span><span class="hs-comment">-- 'Control.Concurrent.Chan.Chan').</span><span>
</span><span id="line-173"></span><span class="hs-comment">--</span><span>
</span><span id="line-174"></span><span class="hs-comment">-- For example, here is how we might implement an [echo</span><span>
</span><span id="line-175"></span><span class="hs-comment">-- server](https://en.wikipedia.org/wiki/Echo_Protocol), using</span><span>
</span><span id="line-176"></span><span class="hs-comment">-- 'forever' both to listen for client connections on a network socket</span><span>
</span><span id="line-177"></span><span class="hs-comment">-- and to echo client input on client connection handles:</span><span>
</span><span id="line-178"></span><span class="hs-comment">--</span><span>
</span><span id="line-179"></span><span class="hs-comment">-- @</span><span>
</span><span id="line-180"></span><span class="hs-comment">-- echoServer :: Socket -&gt; IO ()</span><span>
</span><span id="line-181"></span><span class="hs-comment">-- echoServer socket = 'forever' $ do</span><span>
</span><span id="line-182"></span><span class="hs-comment">--   client &lt;- accept socket</span><span>
</span><span id="line-183"></span><span class="hs-comment">--   'Control.Concurrent.forkFinally' (echo client) (\\_ -&gt; hClose client)</span><span>
</span><span id="line-184"></span><span class="hs-comment">--   where</span><span>
</span><span id="line-185"></span><span class="hs-comment">--     echo :: Handle -&gt; IO ()</span><span>
</span><span id="line-186"></span><span class="hs-comment">--     echo client = 'forever' $</span><span>
</span><span id="line-187"></span><span class="hs-comment">--       hGetLine client &gt;&gt;= hPutStrLn client</span><span>
</span><span id="line-188"></span><span class="hs-comment">-- @</span><span>
</span><span id="line-189"></span><span class="hs-comment">--</span><span>
</span><span id="line-190"></span><span class="hs-comment">-- Note that &quot;forever&quot; isn't necessarily non-terminating.</span><span>
</span><span id="line-191"></span><span class="hs-comment">-- If the action is in a @'MonadPlus'@ and short-circuits after some number of iterations.</span><span>
</span><span id="line-192"></span><span class="hs-comment">-- then @'forever'@ actually returns `mzero`, effectively short-circuiting its caller.</span><span>
</span><span id="line-193"></span><span id="local-6989586621679553510"><span id="local-6989586621679553511"><span id="local-6989586621679553512"><span class="annot"><a href="Control.Monad.html#forever"><span class="hs-identifier hs-type">forever</span></a></span><span>     </span><span class="hs-glyph">::</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Base.html#Applicative"><span class="hs-identifier hs-type">Applicative</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553512"><span class="hs-identifier hs-type">f</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553512"><span class="hs-identifier hs-type">f</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553511"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553512"><span class="hs-identifier hs-type">f</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553510"><span class="hs-identifier hs-type">b</span></a></span></span></span></span><span>
</span><span id="line-194"></span><span class="hs-pragma">{-# INLINE</span><span> </span><span class="annot"><a href="Control.Monad.html#forever"><span class="hs-pragma hs-type">forever</span></a></span><span> </span><span class="hs-pragma">#-}</span><span>
</span><span id="line-195"></span><span id="forever"><span class="annot"><span class="annottext">forever :: forall (f :: * -&gt; *) a b. Applicative f =&gt; f a -&gt; f b
</span><a href="Control.Monad.html#forever"><span class="hs-identifier hs-var hs-var">forever</span></a></span></span><span> </span><span id="local-6989586621679553346"><span class="annot"><span class="annottext">f a
</span><a href="#local-6989586621679553346"><span class="hs-identifier hs-var">a</span></a></span></span><span>   </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">let</span><span> </span><span id="local-6989586621679553344"><span class="annot"><span class="annottext">a' :: f b
</span><a href="#local-6989586621679553344"><span class="hs-identifier hs-var hs-var">a'</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">f a
</span><a href="#local-6989586621679553346"><span class="hs-identifier hs-var">a</span></a></span><span> </span><span class="annot"><span class="annottext">f a -&gt; f b -&gt; f b
forall (f :: * -&gt; *) a b. Applicative f =&gt; f a -&gt; f b -&gt; f b
</span><a href="GHC.Base.html#%2A%3E"><span class="hs-operator hs-var">*&gt;</span></a></span><span> </span><span class="annot"><span class="annottext">f b
</span><a href="#local-6989586621679553344"><span class="hs-identifier hs-var">a'</span></a></span><span> </span><span class="hs-keyword">in</span><span> </span><span class="annot"><span class="annottext">f b
forall {b}. f b
</span><a href="#local-6989586621679553344"><span class="hs-identifier hs-var">a'</span></a></span><span>
</span><span id="line-196"></span><span class="hs-comment">-- Use explicit sharing here, as it prevents a space leak regardless of</span><span>
</span><span id="line-197"></span><span class="hs-comment">-- optimizations.</span><span>
</span><span id="line-198"></span><span>
</span><span id="line-199"></span><span class="hs-comment">-- -----------------------------------------------------------------------------</span><span>
</span><span id="line-200"></span><span class="hs-comment">-- Other monad functions</span><span>
</span><span id="line-201"></span><span>
</span><span id="line-202"></span><span class="hs-comment">-- | The 'mapAndUnzipM' function maps its first argument over a list, returning</span><span>
</span><span id="line-203"></span><span class="hs-comment">-- the result as a pair of lists. This function is mainly used with complicated</span><span>
</span><span id="line-204"></span><span class="hs-comment">-- data structures or a state monad.</span><span>
</span><span id="line-205"></span><span id="local-6989586621679553500"><span id="local-6989586621679553501"><span id="local-6989586621679553502"><span id="local-6989586621679553503"><span class="annot"><a href="Control.Monad.html#mapAndUnzipM"><span class="hs-identifier hs-type">mapAndUnzipM</span></a></span><span>      </span><span class="hs-glyph">::</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Base.html#Applicative"><span class="hs-identifier hs-type">Applicative</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553503"><span class="hs-identifier hs-type">m</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679553502"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553503"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679553501"><span class="hs-identifier hs-type">b</span></a></span><span class="hs-special">,</span><span class="annot"><a href="#local-6989586621679553500"><span class="hs-identifier hs-type">c</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="#local-6989586621679553502"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">]</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553503"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">[</span><span class="annot"><a href="#local-6989586621679553501"><span class="hs-identifier hs-type">b</span></a></span><span class="hs-special">]</span><span class="hs-special">,</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="#local-6989586621679553500"><span class="hs-identifier hs-type">c</span></a></span><span class="hs-special">]</span><span class="hs-special">)</span></span></span></span></span><span>
</span><span id="line-206"></span><span class="hs-pragma">{-# INLINE</span><span> </span><span class="annot"><a href="Control.Monad.html#mapAndUnzipM"><span class="hs-pragma hs-type">mapAndUnzipM</span></a></span><span> </span><span class="hs-pragma">#-}</span><span>
</span><span id="line-207"></span><span class="hs-comment">-- Inline so that fusion with 'unzip' and 'traverse' has a chance to fire.</span><span>
</span><span id="line-208"></span><span class="hs-comment">-- See Note [Inline @unzipN@ functions] in GHC/OldList.hs.</span><span>
</span><span id="line-209"></span><span id="mapAndUnzipM"><span class="annot"><span class="annottext">mapAndUnzipM :: forall (m :: * -&gt; *) a b c.
Applicative m =&gt;
(a -&gt; m (b, c)) -&gt; [a] -&gt; m ([b], [c])
</span><a href="Control.Monad.html#mapAndUnzipM"><span class="hs-identifier hs-var hs-var">mapAndUnzipM</span></a></span></span><span> </span><span id="local-6989586621679553336"><span class="annot"><span class="annottext">a -&gt; m (b, c)
</span><a href="#local-6989586621679553336"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span id="local-6989586621679553335"><span class="annot"><span class="annottext">[a]
</span><a href="#local-6989586621679553335"><span class="hs-identifier hs-var">xs</span></a></span></span><span> </span><span class="hs-glyph">=</span><span>  </span><span class="annot"><span class="annottext">[(b, c)] -&gt; ([b], [c])
forall a b. [(a, b)] -&gt; ([a], [b])
</span><a href="GHC.List.html#unzip"><span class="hs-identifier hs-var">unzip</span></a></span><span> </span><span class="annot"><span class="annottext">([(b, c)] -&gt; ([b], [c])) -&gt; m [(b, c)] -&gt; m ([b], [c])
forall (f :: * -&gt; *) a b. Functor f =&gt; (a -&gt; b) -&gt; f a -&gt; f b
</span><a href="Data.Functor.html#%3C%24%3E"><span class="hs-operator hs-var">&lt;$&gt;</span></a></span><span> </span><span class="annot"><span class="annottext">(a -&gt; m (b, c)) -&gt; [a] -&gt; m [(b, c)]
forall (t :: * -&gt; *) (f :: * -&gt; *) a b.
(Traversable t, Applicative f) =&gt;
(a -&gt; f b) -&gt; t a -&gt; f (t b)
</span><a href="Data.Traversable.html#traverse"><span class="hs-identifier hs-var">traverse</span></a></span><span> </span><span class="annot"><span class="annottext">a -&gt; m (b, c)
</span><a href="#local-6989586621679553336"><span class="hs-identifier hs-var">f</span></a></span><span> </span><span class="annot"><span class="annottext">[a]
</span><a href="#local-6989586621679553335"><span class="hs-identifier hs-var">xs</span></a></span><span>
</span><span id="line-210"></span><span>
</span><span id="line-211"></span><span class="hs-comment">-- | The 'zipWithM' function generalizes 'zipWith' to arbitrary applicative functors.</span><span>
</span><span id="line-212"></span><span id="local-6989586621679553483"><span id="local-6989586621679553484"><span id="local-6989586621679553485"><span id="local-6989586621679553486"><span class="annot"><a href="Control.Monad.html#zipWithM"><span class="hs-identifier hs-type">zipWithM</span></a></span><span>          </span><span class="hs-glyph">::</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Base.html#Applicative"><span class="hs-identifier hs-type">Applicative</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553486"><span class="hs-identifier hs-type">m</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679553485"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553484"><span class="hs-identifier hs-type">b</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553486"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553483"><span class="hs-identifier hs-type">c</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="#local-6989586621679553485"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">]</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="#local-6989586621679553484"><span class="hs-identifier hs-type">b</span></a></span><span class="hs-special">]</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553486"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="hs-special">[</span><span class="annot"><a href="#local-6989586621679553483"><span class="hs-identifier hs-type">c</span></a></span><span class="hs-special">]</span></span></span></span></span><span>
</span><span id="line-213"></span><span class="hs-pragma">{-# INLINE</span><span> </span><span class="annot"><a href="Control.Monad.html#zipWithM"><span class="hs-pragma hs-type">zipWithM</span></a></span><span> </span><span class="hs-pragma">#-}</span><span>
</span><span id="line-214"></span><span class="hs-comment">-- Inline so that fusion with zipWith and sequenceA have a chance to fire</span><span>
</span><span id="line-215"></span><span class="hs-comment">-- See Note [Fusion for zipN/zipWithN] in List.hs]</span><span>
</span><span id="line-216"></span><span id="zipWithM"><span class="annot"><span class="annottext">zipWithM :: forall (m :: * -&gt; *) a b c.
Applicative m =&gt;
(a -&gt; b -&gt; m c) -&gt; [a] -&gt; [b] -&gt; m [c]
</span><a href="Control.Monad.html#zipWithM"><span class="hs-identifier hs-var hs-var">zipWithM</span></a></span></span><span> </span><span id="local-6989586621679553331"><span class="annot"><span class="annottext">a -&gt; b -&gt; m c
</span><a href="#local-6989586621679553331"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span id="local-6989586621679553330"><span class="annot"><span class="annottext">[a]
</span><a href="#local-6989586621679553330"><span class="hs-identifier hs-var">xs</span></a></span></span><span> </span><span id="local-6989586621679553329"><span class="annot"><span class="annottext">[b]
</span><a href="#local-6989586621679553329"><span class="hs-identifier hs-var">ys</span></a></span></span><span>  </span><span class="hs-glyph">=</span><span>  </span><span class="annot"><span class="annottext">[m c] -&gt; m [c]
forall (t :: * -&gt; *) (f :: * -&gt; *) a.
(Traversable t, Applicative f) =&gt;
t (f a) -&gt; f (t a)
</span><a href="Data.Traversable.html#sequenceA"><span class="hs-identifier hs-var">sequenceA</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">(a -&gt; b -&gt; m c) -&gt; [a] -&gt; [b] -&gt; [m c]
forall a b c. (a -&gt; b -&gt; c) -&gt; [a] -&gt; [b] -&gt; [c]
</span><a href="GHC.List.html#zipWith"><span class="hs-identifier hs-var">zipWith</span></a></span><span> </span><span class="annot"><span class="annottext">a -&gt; b -&gt; m c
</span><a href="#local-6989586621679553331"><span class="hs-identifier hs-var">f</span></a></span><span> </span><span class="annot"><span class="annottext">[a]
</span><a href="#local-6989586621679553330"><span class="hs-identifier hs-var">xs</span></a></span><span> </span><span class="annot"><span class="annottext">[b]
</span><a href="#local-6989586621679553329"><span class="hs-identifier hs-var">ys</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-217"></span><span>
</span><span id="line-218"></span><span class="hs-comment">-- | 'zipWithM_' is the extension of 'zipWithM' which ignores the final result.</span><span>
</span><span id="line-219"></span><span id="local-6989586621679553470"><span id="local-6989586621679553471"><span id="local-6989586621679553472"><span id="local-6989586621679553473"><span class="annot"><a href="Control.Monad.html#zipWithM_"><span class="hs-identifier hs-type">zipWithM_</span></a></span><span>         </span><span class="hs-glyph">::</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Base.html#Applicative"><span class="hs-identifier hs-type">Applicative</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553473"><span class="hs-identifier hs-type">m</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679553472"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553471"><span class="hs-identifier hs-type">b</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553473"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553470"><span class="hs-identifier hs-type">c</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="#local-6989586621679553472"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">]</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="#local-6989586621679553471"><span class="hs-identifier hs-type">b</span></a></span><span class="hs-special">]</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553473"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span></span></span></span></span><span>
</span><span id="line-220"></span><span class="hs-pragma">{-# INLINE</span><span> </span><span class="annot"><a href="Control.Monad.html#zipWithM_"><span class="hs-pragma hs-type">zipWithM_</span></a></span><span> </span><span class="hs-pragma">#-}</span><span>
</span><span id="line-221"></span><span class="hs-comment">-- Inline so that fusion with zipWith and sequenceA have a chance to fire</span><span>
</span><span id="line-222"></span><span class="hs-comment">-- See Note [Fusion for zipN/zipWithN] in List.hs]</span><span>
</span><span id="line-223"></span><span id="zipWithM_"><span class="annot"><span class="annottext">zipWithM_ :: forall (m :: * -&gt; *) a b c.
Applicative m =&gt;
(a -&gt; b -&gt; m c) -&gt; [a] -&gt; [b] -&gt; m ()
</span><a href="Control.Monad.html#zipWithM_"><span class="hs-identifier hs-var hs-var">zipWithM_</span></a></span></span><span> </span><span id="local-6989586621679553324"><span class="annot"><span class="annottext">a -&gt; b -&gt; m c
</span><a href="#local-6989586621679553324"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span id="local-6989586621679553323"><span class="annot"><span class="annottext">[a]
</span><a href="#local-6989586621679553323"><span class="hs-identifier hs-var">xs</span></a></span></span><span> </span><span id="local-6989586621679553322"><span class="annot"><span class="annottext">[b]
</span><a href="#local-6989586621679553322"><span class="hs-identifier hs-var">ys</span></a></span></span><span> </span><span class="hs-glyph">=</span><span>  </span><span class="annot"><span class="annottext">[m c] -&gt; m ()
forall (t :: * -&gt; *) (f :: * -&gt; *) a.
(Foldable t, Applicative f) =&gt;
t (f a) -&gt; f ()
</span><a href="Data.Foldable.html#sequenceA_"><span class="hs-identifier hs-var">sequenceA_</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">(a -&gt; b -&gt; m c) -&gt; [a] -&gt; [b] -&gt; [m c]
forall a b c. (a -&gt; b -&gt; c) -&gt; [a] -&gt; [b] -&gt; [c]
</span><a href="GHC.List.html#zipWith"><span class="hs-identifier hs-var">zipWith</span></a></span><span> </span><span class="annot"><span class="annottext">a -&gt; b -&gt; m c
</span><a href="#local-6989586621679553324"><span class="hs-identifier hs-var">f</span></a></span><span> </span><span class="annot"><span class="annottext">[a]
</span><a href="#local-6989586621679553323"><span class="hs-identifier hs-var">xs</span></a></span><span> </span><span class="annot"><span class="annottext">[b]
</span><a href="#local-6989586621679553322"><span class="hs-identifier hs-var">ys</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-224"></span><span>
</span><span id="line-225"></span><span class="hs-comment">{- | The 'foldM' function is analogous to 'Data.Foldable.foldl', except that its result is
encapsulated in a monad. Note that 'foldM' works from left-to-right over
the list arguments. This could be an issue where @('&gt;&gt;')@ and the `folded
function' are not commutative.


&gt; foldM f a1 [x1, x2, ..., xm]
&gt;
&gt; ==
&gt;
&gt; do
&gt;   a2 &lt;- f a1 x1
&gt;   a3 &lt;- f a2 x2
&gt;   ...
&gt;   f am xm

If right-to-left evaluation is required, the input list should be reversed.

Note: 'foldM' is the same as 'foldlM'
-}</span><span>
</span><span id="line-245"></span><span>
</span><span id="line-246"></span><span id="local-6989586621679553459"><span id="local-6989586621679553460"><span id="local-6989586621679553461"><span id="local-6989586621679553462"><span class="annot"><a href="Control.Monad.html#foldM"><span class="hs-identifier hs-type">foldM</span></a></span><span>          </span><span class="hs-glyph">::</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="Data.Foldable.html#Foldable"><span class="hs-identifier hs-type">Foldable</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553462"><span class="hs-identifier hs-type">t</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.Base.html#Monad"><span class="hs-identifier hs-type">Monad</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553461"><span class="hs-identifier hs-type">m</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679553460"><span class="hs-identifier hs-type">b</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553459"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553461"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553460"><span class="hs-identifier hs-type">b</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553460"><span class="hs-identifier hs-type">b</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553462"><span class="hs-identifier hs-type">t</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553459"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553461"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553460"><span class="hs-identifier hs-type">b</span></a></span></span></span></span></span><span>
</span><span id="line-247"></span><span class="hs-pragma">{-# INLINABLE</span><span> </span><span class="annot"><a href="Control.Monad.html#foldM"><span class="hs-pragma hs-type">foldM</span></a></span><span> </span><span class="hs-pragma">#-}</span><span>
</span><span id="line-248"></span><span id="local-6989586621679553320"><span id="local-6989586621679553321"><span class="hs-pragma">{-# SPECIALISE</span><span> </span><span class="annot"><a href="Control.Monad.html#foldM"><span class="hs-pragma hs-type">foldM</span></a></span><span> </span><span class="hs-pragma">::</span><span> </span><span class="hs-pragma">(</span><span class="annot"><a href="#local-6989586621679553321"><span class="hs-pragma hs-type">a</span></a></span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553320"><span class="hs-pragma hs-type">b</span></a></span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-pragma hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553321"><span class="hs-pragma hs-type">a</span></a></span><span class="hs-pragma">)</span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553321"><span class="hs-pragma hs-type">a</span></a></span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="hs-pragma">[</span><span class="annot"><a href="#local-6989586621679553320"><span class="hs-pragma hs-type">b</span></a></span><span class="hs-pragma">]</span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-pragma hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553321"><span class="hs-pragma hs-type">a</span></a></span><span> </span><span class="hs-pragma">#-}</span></span></span><span>
</span><span id="line-249"></span><span id="local-6989586621679553318"><span id="local-6989586621679553319"><span class="hs-pragma">{-# SPECIALISE</span><span> </span><span class="annot"><a href="Control.Monad.html#foldM"><span class="hs-pragma hs-type">foldM</span></a></span><span> </span><span class="hs-pragma">::</span><span> </span><span class="hs-pragma">(</span><span class="annot"><a href="#local-6989586621679553319"><span class="hs-pragma hs-type">a</span></a></span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553318"><span class="hs-pragma hs-type">b</span></a></span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="GHC.Maybe.html#Maybe"><span class="hs-pragma hs-type">Maybe</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553319"><span class="hs-pragma hs-type">a</span></a></span><span class="hs-pragma">)</span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553319"><span class="hs-pragma hs-type">a</span></a></span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="hs-pragma">[</span><span class="annot"><a href="#local-6989586621679553318"><span class="hs-pragma hs-type">b</span></a></span><span class="hs-pragma">]</span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="GHC.Maybe.html#Maybe"><span class="hs-pragma hs-type">Maybe</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553319"><span class="hs-pragma hs-type">a</span></a></span><span> </span><span class="hs-pragma">#-}</span></span></span><span>
</span><span id="line-250"></span><span id="foldM"><span class="annot"><span class="annottext">foldM :: forall (t :: * -&gt; *) (m :: * -&gt; *) b a.
(Foldable t, Monad m) =&gt;
(b -&gt; a -&gt; m b) -&gt; b -&gt; t a -&gt; m b
</span><a href="Control.Monad.html#foldM"><span class="hs-identifier hs-var hs-var">foldM</span></a></span></span><span>          </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(b -&gt; a -&gt; m b) -&gt; b -&gt; t a -&gt; m b
forall (t :: * -&gt; *) (m :: * -&gt; *) b a.
(Foldable t, Monad m) =&gt;
(b -&gt; a -&gt; m b) -&gt; b -&gt; t a -&gt; m b
</span><a href="Data.Foldable.html#foldlM"><span class="hs-identifier hs-var">foldlM</span></a></span><span>
</span><span id="line-251"></span><span>
</span><span id="line-252"></span><span class="hs-comment">-- | Like 'foldM', but discards the result.</span><span>
</span><span id="line-253"></span><span id="local-6989586621679553451"><span id="local-6989586621679553452"><span id="local-6989586621679553453"><span id="local-6989586621679553454"><span class="annot"><a href="Control.Monad.html#foldM_"><span class="hs-identifier hs-type">foldM_</span></a></span><span>         </span><span class="hs-glyph">::</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="Data.Foldable.html#Foldable"><span class="hs-identifier hs-type">Foldable</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553454"><span class="hs-identifier hs-type">t</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.Base.html#Monad"><span class="hs-identifier hs-type">Monad</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553453"><span class="hs-identifier hs-type">m</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679553452"><span class="hs-identifier hs-type">b</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553451"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553453"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553452"><span class="hs-identifier hs-type">b</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553452"><span class="hs-identifier hs-type">b</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553454"><span class="hs-identifier hs-type">t</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553451"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553453"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span></span></span></span></span><span>
</span><span id="line-254"></span><span class="hs-pragma">{-# INLINABLE</span><span> </span><span class="annot"><a href="Control.Monad.html#foldM_"><span class="hs-pragma hs-type">foldM_</span></a></span><span> </span><span class="hs-pragma">#-}</span><span>
</span><span id="line-255"></span><span id="local-6989586621679553312"><span id="local-6989586621679553313"><span class="hs-pragma">{-# SPECIALISE</span><span> </span><span class="annot"><a href="Control.Monad.html#foldM_"><span class="hs-pragma hs-type">foldM_</span></a></span><span> </span><span class="hs-pragma">::</span><span> </span><span class="hs-pragma">(</span><span class="annot"><a href="#local-6989586621679553313"><span class="hs-pragma hs-type">a</span></a></span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553312"><span class="hs-pragma hs-type">b</span></a></span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-pragma hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553313"><span class="hs-pragma hs-type">a</span></a></span><span class="hs-pragma">)</span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553313"><span class="hs-pragma hs-type">a</span></a></span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="hs-pragma">[</span><span class="annot"><a href="#local-6989586621679553312"><span class="hs-pragma hs-type">b</span></a></span><span class="hs-pragma">]</span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-pragma hs-type">IO</span></a></span><span> </span><span class="hs-pragma">(</span><span class="hs-pragma">)</span><span> </span><span class="hs-pragma">#-}</span></span></span><span>
</span><span id="line-256"></span><span id="local-6989586621679553310"><span id="local-6989586621679553311"><span class="hs-pragma">{-# SPECIALISE</span><span> </span><span class="annot"><a href="Control.Monad.html#foldM_"><span class="hs-pragma hs-type">foldM_</span></a></span><span> </span><span class="hs-pragma">::</span><span> </span><span class="hs-pragma">(</span><span class="annot"><a href="#local-6989586621679553311"><span class="hs-pragma hs-type">a</span></a></span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553310"><span class="hs-pragma hs-type">b</span></a></span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="GHC.Maybe.html#Maybe"><span class="hs-pragma hs-type">Maybe</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553311"><span class="hs-pragma hs-type">a</span></a></span><span class="hs-pragma">)</span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553311"><span class="hs-pragma hs-type">a</span></a></span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="hs-pragma">[</span><span class="annot"><a href="#local-6989586621679553310"><span class="hs-pragma hs-type">b</span></a></span><span class="hs-pragma">]</span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="GHC.Maybe.html#Maybe"><span class="hs-pragma hs-type">Maybe</span></a></span><span> </span><span class="hs-pragma">(</span><span class="hs-pragma">)</span><span> </span><span class="hs-pragma">#-}</span></span></span><span>
</span><span id="line-257"></span><span id="foldM_"><span class="annot"><span class="annottext">foldM_ :: forall (t :: * -&gt; *) (m :: * -&gt; *) b a.
(Foldable t, Monad m) =&gt;
(b -&gt; a -&gt; m b) -&gt; b -&gt; t a -&gt; m ()
</span><a href="Control.Monad.html#foldM_"><span class="hs-identifier hs-var hs-var">foldM_</span></a></span></span><span> </span><span id="local-6989586621679553303"><span class="annot"><span class="annottext">b -&gt; a -&gt; m b
</span><a href="#local-6989586621679553303"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span id="local-6989586621679553302"><span class="annot"><span class="annottext">b
</span><a href="#local-6989586621679553302"><span class="hs-identifier hs-var">a</span></a></span></span><span> </span><span id="local-6989586621679553301"><span class="annot"><span class="annottext">t a
</span><a href="#local-6989586621679553301"><span class="hs-identifier hs-var">xs</span></a></span></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(b -&gt; a -&gt; m b) -&gt; b -&gt; t a -&gt; m b
forall (t :: * -&gt; *) (m :: * -&gt; *) b a.
(Foldable t, Monad m) =&gt;
(b -&gt; a -&gt; m b) -&gt; b -&gt; t a -&gt; m b
</span><a href="Data.Foldable.html#foldlM"><span class="hs-identifier hs-var">foldlM</span></a></span><span> </span><span class="annot"><span class="annottext">b -&gt; a -&gt; m b
</span><a href="#local-6989586621679553303"><span class="hs-identifier hs-var">f</span></a></span><span> </span><span class="annot"><span class="annottext">b
</span><a href="#local-6989586621679553302"><span class="hs-identifier hs-var">a</span></a></span><span> </span><span class="annot"><span class="annottext">t a
</span><a href="#local-6989586621679553301"><span class="hs-identifier hs-var">xs</span></a></span><span> </span><span class="annot"><span class="annottext">m b -&gt; m () -&gt; m ()
forall (m :: * -&gt; *) a b. Monad m =&gt; m a -&gt; m b -&gt; m b
</span><a href="GHC.Base.html#%3E%3E"><span class="hs-operator hs-var">&gt;&gt;</span></a></span><span> </span><span class="annot"><span class="annottext">() -&gt; m ()
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-258"></span><span>
</span><span id="line-259"></span><span class="hs-comment">{-
Note [Worker/wrapper transform on replicateM/replicateM_]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The implementations of replicateM and replicateM_ both leverage the
worker/wrapper transform. The simpler implementation of replicateM_, as an
example, would be:

    replicateM_ 0 _ = pure ()
    replicateM_ n f = f *&gt; replicateM_ (n - 1) f

However, the self-recursive nature of this implementation inhibits inlining,
which means we never get to specialise to the action (`f` in the code above).
By contrast, the implementation below with a local loop makes it possible to
inline the entire definition (as happens for foldr, for example) thereby
specialising for the particular action.

For further information, see this issue comment, which includes side-by-side
Core: https://gitlab.haskell.org/ghc/ghc/issues/11795#note_118976
-}</span><span>
</span><span id="line-279"></span><span>
</span><span id="line-280"></span><span class="hs-comment">-- | @'replicateM' n act@ performs the action @act@ @n@ times,</span><span>
</span><span id="line-281"></span><span class="hs-comment">-- and then returns the list of results:</span><span>
</span><span id="line-282"></span><span class="hs-comment">--</span><span>
</span><span id="line-283"></span><span class="hs-comment">-- ==== __Examples__</span><span>
</span><span id="line-284"></span><span class="hs-comment">-- &gt;&gt;&gt; replicateM 3 (putStrLn &quot;a&quot;)</span><span>
</span><span id="line-285"></span><span class="hs-comment">-- a</span><span>
</span><span id="line-286"></span><span class="hs-comment">-- a</span><span>
</span><span id="line-287"></span><span class="hs-comment">-- a</span><span>
</span><span id="line-288"></span><span id="local-6989586621679553442"><span id="local-6989586621679553443"><span class="annot"><a href="Control.Monad.html#replicateM"><span class="hs-identifier hs-type">replicateM</span></a></span><span>        </span><span class="hs-glyph">::</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Base.html#Applicative"><span class="hs-identifier hs-type">Applicative</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553443"><span class="hs-identifier hs-type">m</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-identifier hs-type">Int</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553443"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553442"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553443"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="hs-special">[</span><span class="annot"><a href="#local-6989586621679553442"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">]</span></span></span><span>
</span><span id="line-289"></span><span class="hs-pragma">{-# INLINABLE</span><span> </span><span class="annot"><a href="Control.Monad.html#replicateM"><span class="hs-pragma hs-type">replicateM</span></a></span><span> </span><span class="hs-pragma">#-}</span><span>
</span><span id="line-290"></span><span id="local-6989586621679553300"><span class="hs-pragma">{-# SPECIALISE</span><span> </span><span class="annot"><a href="Control.Monad.html#replicateM"><span class="hs-pragma hs-type">replicateM</span></a></span><span> </span><span class="hs-pragma">::</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-pragma hs-type">Int</span></a></span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-pragma hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553300"><span class="hs-pragma hs-type">a</span></a></span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-pragma hs-type">IO</span></a></span><span> </span><span class="hs-pragma">[</span><span class="annot"><a href="#local-6989586621679553300"><span class="hs-pragma hs-type">a</span></a></span><span class="hs-pragma">]</span><span> </span><span class="hs-pragma">#-}</span></span><span>
</span><span id="line-291"></span><span id="local-6989586621679553299"><span class="hs-pragma">{-# SPECIALISE</span><span> </span><span class="annot"><a href="Control.Monad.html#replicateM"><span class="hs-pragma hs-type">replicateM</span></a></span><span> </span><span class="hs-pragma">::</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-pragma hs-type">Int</span></a></span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="GHC.Maybe.html#Maybe"><span class="hs-pragma hs-type">Maybe</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553299"><span class="hs-pragma hs-type">a</span></a></span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="GHC.Maybe.html#Maybe"><span class="hs-pragma hs-type">Maybe</span></a></span><span> </span><span class="hs-pragma">[</span><span class="annot"><a href="#local-6989586621679553299"><span class="hs-pragma hs-type">a</span></a></span><span class="hs-pragma">]</span><span> </span><span class="hs-pragma">#-}</span></span><span>
</span><span id="line-292"></span><span id="replicateM"><span class="annot"><span class="annottext">replicateM :: forall (m :: * -&gt; *) a. Applicative m =&gt; Int -&gt; m a -&gt; m [a]
</span><a href="Control.Monad.html#replicateM"><span class="hs-identifier hs-var hs-var">replicateM</span></a></span></span><span> </span><span id="local-6989586621679553293"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679553293"><span class="hs-identifier hs-var">cnt0</span></a></span></span><span> </span><span id="local-6989586621679553292"><span class="annot"><span class="annottext">m a
</span><a href="#local-6989586621679553292"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-293"></span><span>    </span><span class="annot"><span class="annottext">Int -&gt; m [a]
forall {t}. (Ord t, Num t) =&gt; t -&gt; m [a]
</span><a href="#local-6989586621679553291"><span class="hs-identifier hs-var">loop</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679553293"><span class="hs-identifier hs-var">cnt0</span></a></span><span>
</span><span id="line-294"></span><span>  </span><span class="hs-keyword">where</span><span>
</span><span id="line-295"></span><span>    </span><span id="local-6989586621679553291"><span class="annot"><span class="annottext">loop :: t -&gt; m [a]
</span><a href="#local-6989586621679553291"><span class="hs-identifier hs-var hs-var">loop</span></a></span></span><span> </span><span id="local-6989586621679553282"><span class="annot"><span class="annottext">t
</span><a href="#local-6989586621679553282"><span class="hs-identifier hs-var">cnt</span></a></span></span><span>
</span><span id="line-296"></span><span>        </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">t
</span><a href="#local-6989586621679553282"><span class="hs-identifier hs-var">cnt</span></a></span><span> </span><span class="annot"><span class="annottext">t -&gt; t -&gt; Bool
forall a. Ord a =&gt; a -&gt; a -&gt; Bool
</span><a href="../../ghc-prim/src/GHC.Classes.html#%3C%3D"><span class="hs-operator hs-var">&lt;=</span></a></span><span> </span><span class="annot"><span class="annottext">t
</span><span class="hs-number">0</span></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">[a] -&gt; m [a]
forall (f :: * -&gt; *) a. Applicative f =&gt; a -&gt; f a
</span><a href="GHC.Base.html#pure"><span class="hs-identifier hs-var">pure</span></a></span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span>
</span><span id="line-297"></span><span>        </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="GHC.Base.html#otherwise"><span class="hs-identifier hs-var">otherwise</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(a -&gt; [a] -&gt; [a]) -&gt; m a -&gt; m [a] -&gt; m [a]
forall (f :: * -&gt; *) a b c.
Applicative f =&gt;
(a -&gt; b -&gt; c) -&gt; f a -&gt; f b -&gt; f c
</span><a href="GHC.Base.html#liftA2"><span class="hs-identifier hs-var">liftA2</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-glyph">:</span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">m a
</span><a href="#local-6989586621679553292"><span class="hs-identifier hs-var">f</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">t -&gt; m [a]
</span><a href="#local-6989586621679553291"><span class="hs-identifier hs-var">loop</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">t
</span><a href="#local-6989586621679553282"><span class="hs-identifier hs-var">cnt</span></a></span><span> </span><span class="annot"><span class="annottext">t -&gt; t -&gt; t
forall a. Num a =&gt; a -&gt; a -&gt; a
</span><a href="GHC.Num.html#-"><span class="hs-glyph hs-var">-</span></a></span><span> </span><span class="annot"><span class="annottext">t
</span><span class="hs-number">1</span></span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-298"></span><span>
</span><span id="line-299"></span><span class="hs-comment">-- | Like 'replicateM', but discards the result.</span><span>
</span><span id="line-300"></span><span id="local-6989586621679553435"><span id="local-6989586621679553436"><span class="annot"><a href="Control.Monad.html#replicateM_"><span class="hs-identifier hs-type">replicateM_</span></a></span><span>       </span><span class="hs-glyph">::</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Base.html#Applicative"><span class="hs-identifier hs-type">Applicative</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553436"><span class="hs-identifier hs-type">m</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-identifier hs-type">Int</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553436"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553435"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553436"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span></span></span><span>
</span><span id="line-301"></span><span class="hs-pragma">{-# INLINABLE</span><span> </span><span class="annot"><a href="Control.Monad.html#replicateM_"><span class="hs-pragma hs-type">replicateM_</span></a></span><span> </span><span class="hs-pragma">#-}</span><span>
</span><span id="line-302"></span><span id="local-6989586621679553280"><span class="hs-pragma">{-# SPECIALISE</span><span> </span><span class="annot"><a href="Control.Monad.html#replicateM_"><span class="hs-pragma hs-type">replicateM_</span></a></span><span> </span><span class="hs-pragma">::</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-pragma hs-type">Int</span></a></span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-pragma hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553280"><span class="hs-pragma hs-type">a</span></a></span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-pragma hs-type">IO</span></a></span><span> </span><span class="hs-pragma">(</span><span class="hs-pragma">)</span><span> </span><span class="hs-pragma">#-}</span></span><span>
</span><span id="line-303"></span><span id="local-6989586621679553279"><span class="hs-pragma">{-# SPECIALISE</span><span> </span><span class="annot"><a href="Control.Monad.html#replicateM_"><span class="hs-pragma hs-type">replicateM_</span></a></span><span> </span><span class="hs-pragma">::</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-pragma hs-type">Int</span></a></span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="GHC.Maybe.html#Maybe"><span class="hs-pragma hs-type">Maybe</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553279"><span class="hs-pragma hs-type">a</span></a></span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="GHC.Maybe.html#Maybe"><span class="hs-pragma hs-type">Maybe</span></a></span><span> </span><span class="hs-pragma">(</span><span class="hs-pragma">)</span><span> </span><span class="hs-pragma">#-}</span></span><span>
</span><span id="line-304"></span><span id="replicateM_"><span class="annot"><span class="annottext">replicateM_ :: forall (m :: * -&gt; *) a. Applicative m =&gt; Int -&gt; m a -&gt; m ()
</span><a href="Control.Monad.html#replicateM_"><span class="hs-identifier hs-var hs-var">replicateM_</span></a></span></span><span> </span><span id="local-6989586621679553275"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679553275"><span class="hs-identifier hs-var">cnt0</span></a></span></span><span> </span><span id="local-6989586621679553274"><span class="annot"><span class="annottext">m a
</span><a href="#local-6989586621679553274"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-305"></span><span>    </span><span class="annot"><span class="annottext">Int -&gt; m ()
forall {t}. (Ord t, Num t) =&gt; t -&gt; m ()
</span><a href="#local-6989586621679553273"><span class="hs-identifier hs-var">loop</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679553275"><span class="hs-identifier hs-var">cnt0</span></a></span><span>
</span><span id="line-306"></span><span>  </span><span class="hs-keyword">where</span><span>
</span><span id="line-307"></span><span>    </span><span id="local-6989586621679553273"><span class="annot"><span class="annottext">loop :: t -&gt; m ()
</span><a href="#local-6989586621679553273"><span class="hs-identifier hs-var hs-var">loop</span></a></span></span><span> </span><span id="local-6989586621679553264"><span class="annot"><span class="annottext">t
</span><a href="#local-6989586621679553264"><span class="hs-identifier hs-var">cnt</span></a></span></span><span>
</span><span id="line-308"></span><span>        </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">t
</span><a href="#local-6989586621679553264"><span class="hs-identifier hs-var">cnt</span></a></span><span> </span><span class="annot"><span class="annottext">t -&gt; t -&gt; Bool
forall a. Ord a =&gt; a -&gt; a -&gt; Bool
</span><a href="../../ghc-prim/src/GHC.Classes.html#%3C%3D"><span class="hs-operator hs-var">&lt;=</span></a></span><span> </span><span class="annot"><span class="annottext">t
</span><span class="hs-number">0</span></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">() -&gt; m ()
forall (f :: * -&gt; *) a. Applicative f =&gt; a -&gt; f a
</span><a href="GHC.Base.html#pure"><span class="hs-identifier hs-var">pure</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-309"></span><span>        </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="GHC.Base.html#otherwise"><span class="hs-identifier hs-var">otherwise</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">m a
</span><a href="#local-6989586621679553274"><span class="hs-identifier hs-var">f</span></a></span><span> </span><span class="annot"><span class="annottext">m a -&gt; m () -&gt; m ()
forall (f :: * -&gt; *) a b. Applicative f =&gt; f a -&gt; f b -&gt; f b
</span><a href="GHC.Base.html#%2A%3E"><span class="hs-operator hs-var">*&gt;</span></a></span><span> </span><span class="annot"><span class="annottext">t -&gt; m ()
</span><a href="#local-6989586621679553273"><span class="hs-identifier hs-var">loop</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">t
</span><a href="#local-6989586621679553264"><span class="hs-identifier hs-var">cnt</span></a></span><span> </span><span class="annot"><span class="annottext">t -&gt; t -&gt; t
forall a. Num a =&gt; a -&gt; a -&gt; a
</span><a href="GHC.Num.html#-"><span class="hs-glyph hs-var">-</span></a></span><span> </span><span class="annot"><span class="annottext">t
</span><span class="hs-number">1</span></span><span class="hs-special">)</span><span>
</span><span id="line-310"></span><span>
</span><span id="line-311"></span><span>
</span><span id="line-312"></span><span class="hs-comment">-- | The reverse of 'when'.</span><span>
</span><span id="line-313"></span><span id="local-6989586621679553431"><span class="annot"><a href="Control.Monad.html#unless"><span class="hs-identifier hs-type">unless</span></a></span><span>            </span><span class="hs-glyph">::</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Base.html#Applicative"><span class="hs-identifier hs-type">Applicative</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553431"><span class="hs-identifier hs-type">f</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Bool"><span class="hs-identifier hs-type">Bool</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553431"><span class="hs-identifier hs-type">f</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553431"><span class="hs-identifier hs-type">f</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span></span><span>
</span><span id="line-314"></span><span class="hs-pragma">{-# INLINABLE</span><span> </span><span class="annot"><a href="Control.Monad.html#unless"><span class="hs-pragma hs-type">unless</span></a></span><span> </span><span class="hs-pragma">#-}</span><span>
</span><span id="line-315"></span><span class="hs-pragma">{-# SPECIALISE</span><span> </span><span class="annot"><a href="Control.Monad.html#unless"><span class="hs-pragma hs-type">unless</span></a></span><span> </span><span class="hs-pragma">::</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Bool"><span class="hs-pragma hs-type">Bool</span></a></span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-pragma hs-type">IO</span></a></span><span> </span><span class="hs-pragma">(</span><span class="hs-pragma">)</span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-pragma hs-type">IO</span></a></span><span> </span><span class="hs-pragma">(</span><span class="hs-pragma">)</span><span> </span><span class="hs-pragma">#-}</span><span>
</span><span id="line-316"></span><span class="hs-pragma">{-# SPECIALISE</span><span> </span><span class="annot"><a href="Control.Monad.html#unless"><span class="hs-pragma hs-type">unless</span></a></span><span> </span><span class="hs-pragma">::</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Bool"><span class="hs-pragma hs-type">Bool</span></a></span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="GHC.Maybe.html#Maybe"><span class="hs-pragma hs-type">Maybe</span></a></span><span> </span><span class="hs-pragma">(</span><span class="hs-pragma">)</span><span> </span><span class="hs-pragma">-&gt;</span><span> </span><span class="annot"><a href="GHC.Maybe.html#Maybe"><span class="hs-pragma hs-type">Maybe</span></a></span><span> </span><span class="hs-pragma">(</span><span class="hs-pragma">)</span><span> </span><span class="hs-pragma">#-}</span><span>
</span><span id="line-317"></span><span id="unless"><span class="annot"><span class="annottext">unless :: forall (f :: * -&gt; *). Applicative f =&gt; Bool -&gt; f () -&gt; f ()
</span><a href="Control.Monad.html#unless"><span class="hs-identifier hs-var hs-var">unless</span></a></span></span><span> </span><span id="local-6989586621679553261"><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679553261"><span class="hs-identifier hs-var">p</span></a></span></span><span> </span><span id="local-6989586621679553260"><span class="annot"><span class="annottext">f ()
</span><a href="#local-6989586621679553260"><span class="hs-identifier hs-var">s</span></a></span></span><span>        </span><span class="hs-glyph">=</span><span>  </span><span class="hs-keyword">if</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679553261"><span class="hs-identifier hs-var">p</span></a></span><span> </span><span class="hs-keyword">then</span><span> </span><span class="annot"><span class="annottext">() -&gt; f ()
forall (f :: * -&gt; *) a. Applicative f =&gt; a -&gt; f a
</span><a href="GHC.Base.html#pure"><span class="hs-identifier hs-var">pure</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-keyword">else</span><span> </span><span class="annot"><span class="annottext">f ()
</span><a href="#local-6989586621679553260"><span class="hs-identifier hs-var">s</span></a></span><span>
</span><span id="line-318"></span><span>
</span><span id="line-319"></span><span class="hs-keyword">infixl</span><span> </span><span class="hs-number">4</span><span> </span><span class="annot"><a href="Control.Monad.html#%3C%24%21%3E"><span class="hs-operator hs-type">&lt;$!&gt;</span></a></span><span>
</span><span id="line-320"></span><span>
</span><span id="line-321"></span><span class="hs-comment">-- | Strict version of 'Data.Functor.&lt;$&gt;'.</span><span>
</span><span id="line-322"></span><span class="hs-comment">--</span><span>
</span><span id="line-323"></span><span class="hs-comment">-- @since 4.8.0.0</span><span>
</span><span id="line-324"></span><span id="local-6989586621679553427"><span id="local-6989586621679553428"><span id="local-6989586621679553429"><span class="annot"><a href="Control.Monad.html#%3C%24%21%3E"><span class="hs-operator hs-type">(&lt;$!&gt;)</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.Base.html#Monad"><span class="hs-identifier hs-type">Monad</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553429"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679553428"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553427"><span class="hs-identifier hs-type">b</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553429"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553428"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553429"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553427"><span class="hs-identifier hs-type">b</span></a></span></span></span></span><span>
</span><span id="line-325"></span><span class="hs-pragma">{-# INLINE</span><span> </span><span class="hs-pragma">(</span><span class="hs-pragma">&lt;$!&gt;</span><span class="hs-pragma">)</span><span> </span><span class="hs-pragma">#-}</span><span>
</span><span id="line-326"></span><span id="local-6989586621679553256"><span class="annot"><span class="annottext">a -&gt; b
</span><a href="#local-6989586621679553256"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span id="%3C%24%21%3E"><span class="annot"><span class="annottext">&lt;$!&gt; :: forall (m :: * -&gt; *) a b. Monad m =&gt; (a -&gt; b) -&gt; m a -&gt; m b
</span><a href="Control.Monad.html#%3C%24%21%3E"><span class="hs-operator hs-var hs-var">&lt;$!&gt;</span></a></span></span><span> </span><span id="local-6989586621679553255"><span class="annot"><span class="annottext">m a
</span><a href="#local-6989586621679553255"><span class="hs-identifier hs-var">m</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-327"></span><span>  </span><span id="local-6989586621679553254"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679553254"><span class="hs-identifier hs-var">x</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">m a
</span><a href="#local-6989586621679553255"><span class="hs-identifier hs-var">m</span></a></span><span>
</span><span id="line-328"></span><span>  </span><span class="hs-keyword">let</span><span> </span><span id="local-6989586621679553253"><span class="annot"><span class="annottext">z :: b
</span><a href="#local-6989586621679553253"><span class="hs-identifier hs-var hs-var">z</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">a -&gt; b
</span><a href="#local-6989586621679553256"><span class="hs-identifier hs-var">f</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679553254"><span class="hs-identifier hs-var">x</span></a></span><span>
</span><span id="line-329"></span><span>  </span><span class="annot"><span class="annottext">b
</span><a href="#local-6989586621679553253"><span class="hs-identifier hs-var">z</span></a></span><span> </span><span class="annot"><span class="annottext">b -&gt; m b -&gt; m b
</span><a href="../../ghc-prim/src/GHC.Prim.html#seq"><span class="hs-operator hs-var">`seq`</span></a></span><span> </span><span class="annot"><span class="annottext">b -&gt; m b
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="annot"><span class="annottext">b
</span><a href="#local-6989586621679553253"><span class="hs-identifier hs-var">z</span></a></span><span>
</span><span id="line-330"></span><span>
</span><span id="line-331"></span><span>
</span><span id="line-332"></span><span class="hs-comment">-- -----------------------------------------------------------------------------</span><span>
</span><span id="line-333"></span><span class="hs-comment">-- Other MonadPlus functions</span><span>
</span><span id="line-334"></span><span>
</span><span id="line-335"></span><span class="hs-comment">-- | Direct 'MonadPlus' equivalent of 'Data.List.filter'.</span><span>
</span><span id="line-336"></span><span class="hs-comment">--</span><span>
</span><span id="line-337"></span><span class="hs-comment">-- ==== __Examples__</span><span>
</span><span id="line-338"></span><span class="hs-comment">--</span><span>
</span><span id="line-339"></span><span class="hs-comment">-- The 'Data.List.filter' function is just 'mfilter' specialized to</span><span>
</span><span id="line-340"></span><span class="hs-comment">-- the list monad:</span><span>
</span><span id="line-341"></span><span class="hs-comment">--</span><span>
</span><span id="line-342"></span><span class="hs-comment">-- @</span><span>
</span><span id="line-343"></span><span class="hs-comment">-- 'Data.List.filter' = ( 'mfilter' :: (a -&gt; Bool) -&gt; [a] -&gt; [a] )</span><span>
</span><span id="line-344"></span><span class="hs-comment">-- @</span><span>
</span><span id="line-345"></span><span class="hs-comment">--</span><span>
</span><span id="line-346"></span><span class="hs-comment">-- An example using 'mfilter' with the 'Maybe' monad:</span><span>
</span><span id="line-347"></span><span class="hs-comment">--</span><span>
</span><span id="line-348"></span><span class="hs-comment">-- @</span><span>
</span><span id="line-349"></span><span class="hs-comment">-- &gt;&gt;&gt; mfilter odd (Just 1)</span><span>
</span><span id="line-350"></span><span class="hs-comment">-- Just 1</span><span>
</span><span id="line-351"></span><span class="hs-comment">-- &gt;&gt;&gt; mfilter odd (Just 2)</span><span>
</span><span id="line-352"></span><span class="hs-comment">-- Nothing</span><span>
</span><span id="line-353"></span><span class="hs-comment">-- @</span><span>
</span><span id="line-354"></span><span id="local-6989586621679553419"><span id="local-6989586621679553421"><span class="annot"><a href="Control.Monad.html#mfilter"><span class="hs-identifier hs-type">mfilter</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Base.html#MonadPlus"><span class="hs-identifier hs-type">MonadPlus</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553421"><span class="hs-identifier hs-type">m</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679553419"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Bool"><span class="hs-identifier hs-type">Bool</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553421"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553419"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679553421"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679553419"><span class="hs-identifier hs-type">a</span></a></span></span></span><span>
</span><span id="line-355"></span><span class="hs-pragma">{-# INLINABLE</span><span> </span><span class="annot"><a href="Control.Monad.html#mfilter"><span class="hs-pragma hs-type">mfilter</span></a></span><span> </span><span class="hs-pragma">#-}</span><span>
</span><span id="line-356"></span><span id="mfilter"><span class="annot"><span class="annottext">mfilter :: forall (m :: * -&gt; *) a. MonadPlus m =&gt; (a -&gt; Bool) -&gt; m a -&gt; m a
</span><a href="Control.Monad.html#mfilter"><span class="hs-identifier hs-var hs-var">mfilter</span></a></span></span><span> </span><span id="local-6989586621679553246"><span class="annot"><span class="annottext">a -&gt; Bool
</span><a href="#local-6989586621679553246"><span class="hs-identifier hs-var">p</span></a></span></span><span> </span><span id="local-6989586621679553245"><span class="annot"><span class="annottext">m a
</span><a href="#local-6989586621679553245"><span class="hs-identifier hs-var">ma</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-357"></span><span>  </span><span id="local-6989586621679553244"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679553244"><span class="hs-identifier hs-var">a</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">m a
</span><a href="#local-6989586621679553245"><span class="hs-identifier hs-var">ma</span></a></span><span>
</span><span id="line-358"></span><span>  </span><span class="hs-keyword">if</span><span> </span><span class="annot"><span class="annottext">a -&gt; Bool
</span><a href="#local-6989586621679553246"><span class="hs-identifier hs-var">p</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679553244"><span class="hs-identifier hs-var">a</span></a></span><span> </span><span class="hs-keyword">then</span><span> </span><span class="annot"><span class="annottext">a -&gt; m a
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679553244"><span class="hs-identifier hs-var">a</span></a></span><span> </span><span class="hs-keyword">else</span><span> </span><span class="annot"><span class="annottext">m a
forall (m :: * -&gt; *) a. MonadPlus m =&gt; m a
</span><a href="GHC.Base.html#mzero"><span class="hs-identifier hs-var">mzero</span></a></span><span>
</span><span id="line-359"></span><span>
</span><span id="line-360"></span><span class="hs-comment">{- $naming

The functions in this library use the following naming conventions:

* A postfix \'@M@\' always stands for a function in the Kleisli category:
  The monad type constructor @m@ is added to function results
  (modulo currying) and nowhere else.  So, for example,

&gt; filter  ::              (a -&gt;   Bool) -&gt; [a] -&gt;   [a]
&gt; filterM :: (Monad m) =&gt; (a -&gt; m Bool) -&gt; [a] -&gt; m [a]

* A postfix \'@_@\' changes the result type from @(m a)@ to @(m ())@.
  Thus, for example:

&gt; sequence  :: Monad m =&gt; [m a] -&gt; m [a]
&gt; sequence_ :: Monad m =&gt; [m a] -&gt; m ()

* A prefix \'@m@\' generalizes an existing function to a monadic form.
  Thus, for example:

&gt; filter  ::                (a -&gt; Bool) -&gt; [a] -&gt; [a]
&gt; mfilter :: MonadPlus m =&gt; (a -&gt; Bool) -&gt; m a -&gt; m a

-}</span><span>
</span><span id="line-384"></span></pre></body></html>